package com.lonely.framework.config;

import org.redisson.Redisson;
import org.redisson.api.RedissonClient;
import org.redisson.config.Config;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * redisson配置
 *
 * @author fzzf
 */
@Configuration
public class RedissonConfig {

    @Value("${spring.redis.host}")
    private String host;

    @Value("${spring.redis.port}")
    private int port;

    @Value("${spring.redis.password:}")
    private String password;

    @Value("${spring.redis.database:0}")
    private int database;


    /**
     * redisson 客户端对象
     *
     * @return
     */
    @Bean(destroyMethod = "shutdown")
    public RedissonClient redissonClient() {
        Config config = new Config();

        // 单节点模式
        config.useSingleServer()
                .setAddress("redis://" + host + ":" + port)
                .setDatabase(database)
                .setPassword(password.isEmpty() ? null : password);

        // 集群模式（如果需要）
        // config.useClusterServers()
        //       .addNodeAddress("redis://node1:6379", "redis://node2:6379")
        //       .setPassword(password);

        // 哨兵模式（如果需要）
        // config.useSentinelServers()
        //       .addSentinelAddress("redis://sentinel1:26379", "redis://sentinel2:26379")
        //       .setMasterName("mymaster")
        //       .setDatabase(database)
        //       .setPassword(password);

        return Redisson.create(config);
    }
}